package com.example.webone.config;

//全面拓展Spring MVC

import com.example.webone.Intercepter.LoginHandlerInterceptor;
import com.example.webone.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

/*
如果想要自定义一些定制化功能，只要写这个组件，
然后将它交给springboot，spring boot就会帮我们自动装配
 */
@Configuration
//@EnableWebMvc //这个注解就是导入一个类:DelegatingWebMvcConfiguration(从容器中获取所有的webmvcconfig)
/*
 public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }
    }
 */
public class MyMvcConfig implements WebMvcConfigurer {

//   //ViewResolver 实现了视图解析器接口的类，我们就可以把它看做视图解析器
//    @Bean
//    public ViewResolver myViewResolver(){
//        return new MyViewResolver();
//    }
//
//
//    //自定义了一个自己的视图解析器MyViewResolver
//    public static class MyViewResolver implements ViewResolver{
//        @Override
//        public View resolveViewName(String viewName, Locale locale) throws Exception {
//            return null;
//        }
//    }

    //视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/bluesky").setViewName("test");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard");
    }

    //拦截器
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 注册拦截器，及拦截请求和要剔除哪些请求!
//        // 我们还需要过滤静态资源文件，否则样式显示不出来
//        registry.addInterceptor(new LoginHandlerInterceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns("/index.html","/user/login","/","/css/*","/img/**","/js/**");
//    }


    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

